package com.zc.framework.utils;

import org.apache.commons.net.ftp.FTPFile;

import java.io.File;
import java.io.IOException;

/**
 * Created by zc on 2016/9/12.
 */
public class FtpClientUtil {

    private static final int FTP_OPERATION_COUNT = 3;


    public static FtpHelper.UploadStatus sendFile2FtpServer(String host, int port, String userName, String password,
                                                            String localFilePath, String remoteFilePath) throws IOException {
        FtpHelper ftpHelper = new FtpHelper();
        FtpHelper.UploadStatus UploadStatus = FtpHelper.UploadStatus.Upload_New_File_Failed;
        int count = 0;
        if (ftpHelper.connect(host, port, userName, password)) {
            while (!UploadStatus.equals(FtpHelper.UploadStatus.Upload_New_File_Success) && count < FTP_OPERATION_COUNT) {
                UploadStatus = ftpHelper.upload(localFilePath, remoteFilePath);
                count++;
            }
            ftpHelper.disconnect();
        }
        return UploadStatus;
    }

    /**
     *
     */
    public static void createSingleDirectory(String host, int port, String userName, String password,
                                             String ftpDirectory) throws IOException {
        FtpHelper ftpHelper = new FtpHelper();
        if (ftpHelper.connect(host, port, userName, password)) {
            boolean createFlag = ftpHelper.getFtpClient().makeDirectory(ftpDirectory);

            if (!createFlag) {
                ftpHelper.delRemoteDirectory((ftpDirectory), true);
                ftpHelper.getFtpClient().makeDirectory(ftpDirectory);
            }
        }
        ftpHelper.disconnect();
    }
    public static void createNestedDirectory(String host, int port, String userName, String password,
                                             String ftpDirectory) throws IOException {
        FtpHelper ftpHelper = new FtpHelper();
        if (ftpHelper.connect(host, port, userName, password)) {
            FtpHelper.UploadStatus createFlag = ftpHelper.createDirecroty(ftpDirectory, ftpHelper.getFtpClient());//.getFtpClient().makeDirectory(ftpDirectory);

            if (createFlag==FtpHelper.UploadStatus.Create_Directory_Fail) {
                ftpHelper.delRemoteDirectory((ftpDirectory), true);
                ftpHelper.getFtpClient().makeDirectory(ftpDirectory);
            }
        }
        ftpHelper.disconnect();
    }
    public static FtpHelper.UploadStatus sendSingleFile2FtpServer(String host, int port, String userName, String password,
                                                        String localFilePath, String remoteFilePath) throws IOException {
        FtpHelper ftpHelper = new FtpHelper();
        FtpHelper.UploadStatus UploadStatus = FtpHelper.UploadStatus.Upload_New_File_Failed;
        int count = 0;

        if (ftpHelper.connect(host, port, userName, password)) {
            String ftpDirectory = remoteFilePath.substring(0, remoteFilePath.lastIndexOf("/"));
            String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/") + 1);
            boolean flag = false;
            FTPFile[] ftpFiles = ftpHelper.getFtpClient().listFiles(ftpDirectory);
            for (FTPFile f : ftpFiles) {
                if (fileName.equals(f.getName())) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                ftpHelper.getFtpClient().deleteFile(remoteFilePath);
            }
            while (!UploadStatus.equals(FtpHelper.UploadStatus.Upload_New_File_Success) && count < FTP_OPERATION_COUNT) {
                UploadStatus = ftpHelper.upload(localFilePath, remoteFilePath);
                count++;
            }
            ftpHelper.disconnect();
        }
        return UploadStatus;
    }


    public static FtpHelper.DeleteSatsus del(String host, int port, String userName, String password, String remoteFilePath)
            throws IOException {
        FtpHelper ftpHelper = new FtpHelper();
        FtpHelper.DeleteSatsus deleStatus = FtpHelper.DeleteSatsus.Delete_Remote_File_Faild;
        if (ftpHelper.connect(host, port, userName, password)) {
            deleStatus = ftpHelper.delRemoteFile(remoteFilePath);
            ftpHelper.disconnect();
        }
        return deleStatus;
    }

    public static void getFileNames(String host, int port, String userName, String password, String directory)
            throws IOException {
        FtpHelper ftpHelper = new FtpHelper();
        FtpHelper.DeleteSatsus deleStatus = FtpHelper.DeleteSatsus.Remove_Directory_Fail;
        if (ftpHelper.connect(host, port, userName, password)) {
            ftpHelper.getFiles(directory);
            ftpHelper.disconnect();
        }
        return ;
    }

    public static FtpHelper.DeleteSatsus delDirectory(String host, int port, String userName, String password, String directory)
            throws IOException {
        FtpHelper ftpHelper = new FtpHelper();
        FtpHelper.DeleteSatsus deleStatus = FtpHelper.DeleteSatsus.Remove_Directory_Fail;
        if (ftpHelper.connect(host, port, userName, password)) {
            deleStatus = ftpHelper.delRemoteDirectory(directory, true);
            ftpHelper.disconnect();
        }
        return deleStatus;
    }

    public static void replaceFtpFiles(String host, int port, String userName, String password, File[] files,
                                       String dest) throws IOException {

        FtpHelper ftpHelper = new FtpHelper();

        FtpHelper.DeleteSatsus deleteStatus = FtpHelper.DeleteSatsus.Delete_Remote_File_Faild;
        FtpHelper.UploadStatus UploadStatus = FtpHelper.UploadStatus.Upload_New_File_Failed;
        int count = 0;
        if (ftpHelper.connect(host, port, userName, password)) {

            for (int i = 0; i < files.length; i++) {
                if (1 == ftpHelper.listFiles("/" + dest + files[i].getName()).length) {
                    deleteStatus = ftpHelper.delRemoteFile("/" + dest + files[i].getName());
                } else {
                    deleteStatus = FtpHelper.DeleteSatsus.Delete_Remote_File_Success;
                }

                while (!UploadStatus.equals(FtpHelper.UploadStatus.Upload_New_File_Success) && count < FTP_OPERATION_COUNT) {
                    UploadStatus = ftpHelper.upload(files[i].getAbsolutePath(), "/" + dest + files[i].getName());
                    count++;
                }
                if (!deleteStatus.equals(FtpHelper.DeleteSatsus.Delete_Remote_File_Success)) {
                    throw new IOException("delete dir eoor");
                }

                count = 0;
                deleteStatus = FtpHelper.DeleteSatsus.Delete_Remote_File_Faild;
                UploadStatus = FtpHelper.UploadStatus.Upload_New_File_Failed;
            }
            ftpHelper.disconnect();
        }

    }

    public static void uploadFtpFiles(String host, int port, String userName, String password, File[] files, String dest)
            throws IOException {

        FtpHelper ftpHelper = new FtpHelper();

        FtpHelper.DeleteSatsus deleteStatus = FtpHelper.DeleteSatsus.Remove_Directory_Fail;
        FtpHelper.UploadStatus UploadStatus = FtpHelper.UploadStatus.Upload_New_File_Failed;

        if (ftpHelper.connect(host, port, userName, password)) {
            if (ftpHelper.listFiles(dest).length > 0) {
                deleteStatus = ftpHelper.delRemoteDirectory(dest, false);
            } else {
                deleteStatus = FtpHelper.DeleteSatsus.Remove_Directory_Success;
            }
        }
        ftpHelper.disconnect();

        int count = 0;
        if (ftpHelper.connect(host, port, userName, password)) {

            for (int i = 0; i < files.length; i++) {

                while (!UploadStatus.equals(FtpHelper.UploadStatus.Upload_New_File_Success) && count < FTP_OPERATION_COUNT) {
                    UploadStatus = ftpHelper.upload(files[i].getAbsolutePath(), "/" + dest + files[i].getName());
                    count++;
                }
                if (!deleteStatus.equals(FtpHelper.DeleteSatsus.Remove_Directory_Success)) {
                    throw new IOException("delete dir error");
                }

                count = 0;

                UploadStatus = FtpHelper.UploadStatus.Upload_New_File_Failed;
            }
            ftpHelper.disconnect();
        }
        deleteStatus = FtpHelper.DeleteSatsus.Remove_Directory_Fail;
    }
    public static FtpHelper.UploadStatus sendDir2FtpServer(String host, int port, String userName, String password,
                                                 String localDir, String remoteFilePath) throws IOException {
        FtpHelper ftpHelper = new FtpHelper();
        FtpHelper.UploadStatus UploadStatus = FtpHelper.UploadStatus.Upload_New_File_Failed;
        int count = 0;
        if (ftpHelper.connect(host, port, userName, password)) {
            while (!UploadStatus.equals(FtpHelper.UploadStatus.Upload_New_File_Success) && count < FTP_OPERATION_COUNT) {
                UploadStatus = ftpHelper.uploadDir(localDir, remoteFilePath);
                count++;
            }
            ftpHelper.disconnect();
        }
        return UploadStatus;
    }
}
